package com.qqzone.qq.service.impl;

import com.qqzone.qq.dao.ReplyDao;
import com.qqzone.qq.pojo.HostReply;
import com.qqzone.qq.pojo.Reply;
import com.qqzone.qq.pojo.Topic;
import com.qqzone.qq.pojo.UserBasic;
import com.qqzone.qq.service.HostReplyService;
import com.qqzone.qq.service.ReplyService;
import com.qqzone.qq.service.UserBasicService;

import java.util.List;

public class ReplyServiceImpl implements ReplyService {

    private ReplyDao replyDao;
    private HostReplyService hostReplyService;
    private UserBasicService userBasicService;


    @Override
    public void addReply(Reply reply) {
        replyDao.addReply(reply);
    }

    @Override
    public List<Reply> getReplyListByTopicId(Integer topicId) {
        List<Reply> replyList = replyDao.getReplyList(new Topic(topicId));
        for (int i = 0; i < replyList.size(); i++) {
            Reply reply = replyList.get(i);
            HostReply hostReply = hostReplyService.getHotReplyByReplyId(reply.getId());
            reply.setHostReply(hostReply);
            UserBasic author = userBasicService.getUserBasicById(reply.getAuthor().getId());
            reply.setAuthor(author);
        }
        return replyList;
    }

    public Reply getReplyById(Integer id) {
        return replyDao.getReply(id);
    }

    @Override
    public void delReply(Integer id) {
        Reply reply = getReplyById(id);
        HostReply hostReply = hostReplyService.getHotReplyByReplyId(reply.getId());
        if (hostReply != null) {
            hostReplyService.delHostReplyByReplyId(reply.getId());
        }
    }


}
